/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javamailclient;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

/**
 *
 * @author Abigail
 */
public class MyClientMail {

    private MailUtilities mailUtilities;
    private String from;
    private String to;
    private String subject;
    private String sendDate;
    private String textPlainContent;
    private String htmlContent;
    private String contentType;//="text/plain";
    public List<MyAttachment> attachments = new ArrayList<MyAttachment>();

    public MyClientMail Receive(int i) {
        MyClientMail myClientMail = new MyClientMail();

        return myClientMail;
    }

    public void getEnvelope(Message msg) {
//        String from = "";
//        String subject = "";
//        String sendDate = "";
//        String to = "";

        try {
            //IMAPMessage
            //POP3MESSAGE
            String[] a;

            // FROM
            if (msg.getHeader("from") != null) {
                a = msg.getHeader("from");
                for (int j = 0; j < a.length; j++) {
                    setFrom(a[j].toString());
                }
            } else {
                setFrom("<ubknown>");
            }


            // TO
            if (msg.getHeader("to") != null) {
                a = msg.getHeader("to");
                for (int j = 0; j < a.length; j++) {
                    setTo(a[j].toString());
                }
            } else {
                setTo("<unknown>");
            }


            // SUBJECT
            if (msg.getHeader("subject") != null) {//(msg.getSubject() == null || msg.getSubject().equals("")) {
                a = msg.getHeader("subject");
                for (int j = 0; j < a.length; j++) {
                    if (a[j].toString().equals("") == false) {
                        setSubject(a[j].toString());
                    } else {
                        setSubject("<no subject>");
                    }
                }
            } else {
                setSubject("<no subject>");
            }

            // DATE
//            if (msg.getHeader("date") != null) {
//                a = msg.getHeader("date");
//                for (int j = 0; j < a.length; j++) {
//                    sendDate += a[j].toString();
//                }
//                Date d = new Date(sendDate);
//                SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy HH:mm:ss");
//                sendDate = (d != null ? sdf.format(d) : "<unknown>");
//
//            } else {
//                sendDate = "<unknown>";
//            }
            Date d = msg.getSentDate();
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy HH:mm:ss");
            //String df = sdf.format(d);
            setSendDate(d != null ? sdf.format(d) : "<unknown>");

            setFrom(MimeUtility.decodeText(getFrom()));
            setTo(MimeUtility.decodeText(getTo()));
            setSubject(MimeUtility.decodeText(getSubject()));


        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            //showError(ex.getMessage(), false);
        } catch (MessagingException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            //showError(ex.getMessage(), false);
        }
    }

    //<editor-fold defaultstate="collapsed" desc="Encapsulate Field">
    /**
     * @return the mailUtilities
     */
    public MailUtilities getMailUtilities() {
        return mailUtilities;
    }

    /**
     * @param mailUtilities the mailUtilities to set
     */
    public void setMailUtilities(MailUtilities mailUtilities) {
        this.mailUtilities = mailUtilities;
    }

    /**
     * @return the from
     */
    public String getFrom() {
        return from;
    }

    /**
     * @param from the from to set
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * @return the to
     */
    public String getTo() {
        return to;
    }

    /**
     * @param to the to to set
     */
    public void setTo(String to) {
        this.to = to;
    }

    /**
     * @return the subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * @return the sendDate
     */
    public String getSendDate() {
        return sendDate;
    }

    /**
     * @param sendDate the sendDate to set
     */
    public void setSendDate(String sendDate) {
        this.sendDate = sendDate;
    }

    /**
     * @return the textPlainContent
     */
    public String getTextPlainContent() {
        return textPlainContent;
    }

    /**
     * @param textPlainContent the textPlainContent to set
     */
    public void setTextPlainContent(String textPlainContent) {
        this.textPlainContent = textPlainContent;
    }

    /**
     * @return the htmlContent
     */
    public String getHtmlContent() {
        return htmlContent;
    }

    /**
     * @param htmlContent the htmlContent to set
     */
    public void setHtmlContent(String htmlContent) {
        this.htmlContent = htmlContent;
    }
    //</editor-fold>

    /**
     * @return the contentType
     */
    public String getContentType() {
        return contentType;
    }

    /**
     * @param contentType the contentType to set
     */
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}
